<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8 0008
 * Time: 10:10
 */

namespace app\api\controller;


use app\admin\model\Memberrule;
use app\admin\model\Membertype;
use app\common\model\User;
use app\admin\model\Usermemberhistory;

class Member extends BaseController
{
    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查找商家会员
     */
    public function getTenantMemberType()
    {
        $param = self::getHttpParam();
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能这空');
        } else {
            $tenantId = $param->tenantId;
            $memberType = Membertype::where(['tenantId'=>$tenantId])->select();
            if (empty($memberType)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查找会员卡');
            } else {
                for ($i = 0; $i < count($memberType); $i++) {
                    $memberRule = Memberrule::where('id', $memberType[$i]->memRuleId)->find();
                    $memberType[$i]['rule'] = $memberRule;
                }
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $memberType);
            }
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查看个人会员信息
     */
    public function getUserMemberType()
    {
        $param = self::getHttpParam();
        if (empty($param->typeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId不能为空');
        }
        $typeId = $param->typeId;
        $userId = \app\api\service\Token::getCurrentUid();
        $memberType = Usermemberhistory::where(['membertype_id'=>$typeId,'userId'=>$userId])->find();
        if (empty($memberType)) {
            return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到会员卡');
        }
        $find = Membertype::where('id', $typeId)->find();
        $memberType['img'] = $find ? $find['img'] : '';
        $memberType['rule'] = [
            'consumeIntegral' => $memberType['consumeIntegral'] ? : 0,  //消费获得积分
            'consumeMoney' => $memberType['consumeMoney'] ? : 0,  //消费金额
            'isMemBerShop' => $memberType['isMemBerShop'],  //会员卡折扣类型
            'memberDiscounts' => $memberType['memberDiscounts'],  //会员折扣（历史）
            'rechargeIntegral' => $memberType['rechargeIntegral'],  //开通条件（历史）
            'rechargeOpenMoney' => $memberType['rechargeOpenMoney'],  //
            'returnMoney' => $memberType['returnMoney'],
            'memberPointStatus' => $memberType['consumeIntegral'] ? 1 : 2
        ];
        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $memberType);

        // $memberType = Membertype::where('id', $typeId)->find();
        // if (empty($memberType)) {
        //     return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查找会员卡');
        // } else {
        //     $memberRule = Memberrule::where('id', $memberType->memRuleId)->find();
        //     $user = User::where(['id'=>\app\api\service\Token::getCurrentUid(),'typeId'=>$typeId])->find();
        //     $memberType['rule'] = $memberRule;
        //     $memberType['user'] = $user;
        //     return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $memberType);
        // }
    }
}